package org.john.message;

import net.sf.json.JSONObject;

public class JsonMessage extends Message implements Packageable{
	private String jsonString;
	private Packageable packageable;
	
	public void setJsonString(String jsonString){
		this.jsonString = jsonString;
	}
	
	public void SetPackageable(Packageable packageable){
		this.packageable = packageable;
	}
	
	public JsonMessage(String jsonString) {
		super();
		this.jsonString = jsonString;
	}
	
	public Message packagee(){
		if(this.packageable == null){
			return this.packagee(jsonString);
		}else{
			return this.packageable.packagee(jsonString);
		}
	}
	
	
	
	public JsonMessage(Message message){
		Header header = message.getHeader();
		RestMethod restMethod = header.getRestMethod();
		switch (restMethod) {
		case pull:
			header.setRestMethod(RestMethod.push);
			break;
		case push:
			header.setRestMethod(RestMethod.pull);
			break;
		default:
			break;
		}
		this.setHeader(header);
	}

	@Override
	public Message packagee(Object object) {
		String string  = (String) object;
		JSONObject jsonObject = JSONObject.fromObject(string);
		
		JSONObject headerJsonObject = jsonObject.getJSONObject("header");
		
		Header header = new JsonHeader();
		
		String mineTypeString = headerJsonObject.getString("MineType");
	
		if("JsonTextOnly" == mineTypeString){
			header.setMineType(MineType.JsonTextOnly);
		}else if("Image" == mineTypeString){
			header.setMineType(MineType.Image);
		}else if("JsonTextAndImageMerge" == mineTypeString ){
			header.setMineType(MineType.JsonTextAndImageMerge);
		}else if("Media" == mineTypeString){
			header.setMineType(MineType.Media);
		}else if("JsonTextAndMediaMerge" == mineTypeString){
			header.setMineType(MineType.JsonTextAndMediaMerge);
		}
		
		String restMethodString = headerJsonObject.getString("restMethod");
		
		if("push" == restMethodString){
			header.setRestMethod(RestMethod.push);
		}else if("pull" == restMethodString){
			header.setRestMethod(RestMethod.pull);
		}else if("command" == restMethodString){
			header.setRestMethod(RestMethod.command);
		}
		 this.setHeader(header);
		 
		 Body body = new JsonBody();
		 
		 String bodyString = jsonObject.getString("body");
		 body.setContent(bodyString);
		 body.setLength(bodyString.getBytes().length);
		 this.setBody(body);
		return this;
	}

	@Override
	public Object unpackage() {
		JSONObject jsonObject = new JSONObject();
		JSONObject headerObject = new JSONObject();
		JSONObject bodyObject = new JSONObject();
			Header header = getHeader();
			Body body = getBody();
			if(header != null){
				headerObject.put("MineType", header.getMineType().toString());
				headerObject.put("restMethod", header.getRestMethod().toString());
			}
			if(body!=null){
				bodyObject.put("content", body.getContent());
				bodyObject.put("length", body.getLength());
			}
			jsonObject.put("header", headerObject);
			jsonObject.put("body", bodyObject);
			return jsonObject.toString();
	}



	
	
	
}
